<template>
  <el-form :model="formData" :rules="formRules" ref="formRef">
    <el-form-item prop="content" style="margin-bottom: 2px">
      <el-input v-model="formData.content" type="textarea" placeholder="请输入内容"
                autosize></el-input>
    </el-form-item>
    <el-form-item style="text-align: right; margin-bottom: 2px">
      <el-button size="small" @click="cancel">取消</el-button>
      <el-button size="small" type="primary" @click="handleSubmit"> 回复</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { reqAddComment } from '@/api/consumerApi'
import { mapState } from 'vuex'

export default {
  props: ['rootCommentId','info'],
  data() {
    return {
      formData: {
        content: ''
      },
      formRules: {
        content: [
          { required: true, message: "评论内容不能为空！" },
          { min: 0, max: 100, message: "评论内容不超过100字！" }
        ]
      },
    }
  },
  computed: {
    ...mapState('user', [
      'isLogin'
    ])
  },
  methods: {
    // 回复博客
    handleSubmit() {
      this.$refs.formRef.validate(async valid => {
        if (!valid) return
        if (!this.isLogin) {
          this.$message.error("请您登录后再发表评论！");
          return
        }
        let params = {
          content: this.formData.content,
          blogId: this.info.blogId,
          rootCommentId: this.rootCommentId,
          toCommentId: this.info.id,
        }
        reqAddComment(params).then(data => {
          this.$message.success("发表评论成功")
          this.$emit('complete')
        })

      })
    },
    cancel() {
      this.$emit('quit')
    }
  }
}
</script>

<style scoped>

</style>
